package com.ssy.lingxi.product.controller.pc.platform;

import com.ssy.lingxi.common.constant.tree.Node;
import com.ssy.lingxi.common.controller.BaseController;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.product.api.model.request.AttributeValueRequest;
import com.ssy.lingxi.product.api.model.request.SimpleIdRequest;
import com.ssy.lingxi.product.api.model.request.SimpleStatusRequest;
import com.ssy.lingxi.product.api.model.response.AttributeValueResponse;
import com.ssy.lingxi.product.entity.platform.AttributeValue;
import com.ssy.lingxi.product.service.platform.IAttributeValueService;
import org.modelmapper.TypeToken;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @menu: 平台后台--属性值管理
 * 平台后台--属性值管理
 * @author jw.chen
 * @version 2.0.0
 * @date 2020/6/23
 */
@RestController
@RequestMapping("/product/platform")
public class AttributeValueController extends BaseController {
    
    @Resource
    private IAttributeValueService attributeValueService;

    /**
     * 查询属性值树
     * @return Wrapper<List<Node>>
     */
    @RequestMapping(value = "/getAttributeValueTree", method = RequestMethod.GET)
    public Wrapper<List<Node>> getAttributeValueTree() {
        return Wrapper.success(attributeValueService.getAttributeValueTree());
    }

    /**
     * 查询属性值列表
     * @param isEnable 是否有效
     * @param attributeId 属性id
     * @param pageVO 分页实体
     * @param name 属性值名称
     * @return
     */
    @RequestMapping(value = "/getAttributeValueList", method = RequestMethod.GET)
    public Wrapper<PageData<AttributeValueResponse>> getAttributeValueList(PageVO pageVO, @RequestParam(value = "isEnable", required = false) Boolean isEnable, @RequestParam("attributeId") Long attributeId, @RequestParam("name") String name) {
        Page<AttributeValue> result = attributeValueService.getAttributeValueList(pageVO, isEnable, attributeId, name);
        List<AttributeValueResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<AttributeValueResponse>>(){}.getType());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 查询同步属性值列表
     * @param attributeGroupName 属性组名称
     * @param attributeName 属性名称
     * @param attributeValue 属性值
     * @param pageVO 分页实体
     * @return
     */
    @RequestMapping(value = "/getSyncAttributeValueList", method = RequestMethod.GET)
    public Wrapper<PageData<AttributeValueResponse>> getSyncAttributeValueList(PageVO pageVO, @RequestParam(value = "attributeGroupName", required = false) String attributeGroupName, @RequestParam(value = "attributeName", required = false) String attributeName, @RequestParam(value = "attributeValue", required = false) String attributeValue) {
        Page<AttributeValue> result = attributeValueService.getSyncAttributeValueList(pageVO, attributeGroupName, attributeName, attributeValue);
        List<AttributeValueResponse> resultList = this.modelMapper.map(result.getContent(), new TypeToken<List<AttributeValueResponse>>(){}.getType());
        return Wrapper.success(new PageData<>(result.getTotalElements(), resultList));
    }

    /**
     * 查询属性值信息
     * @param id 属性值id
     * @return Wrapper<AttributeValue>
     */
    @RequestMapping(value = "/getAttributeValue", method = RequestMethod.GET)
    public Wrapper<AttributeValueResponse> getAttributeValue(@RequestParam("id") Long id) {
        AttributeValue attributeValue = attributeValueService.getAttributeValue(id);
        if(attributeValue != null){
            return Wrapper.success(this.modelMapper.map(attributeValue, AttributeValueResponse.class));
        }else{
            return Wrapper.success(null);
        }
    }

    /**
     * 新增/修改属性值
     * @param attributeValueRequest
     * @return
     */
    @RequestMapping(value = "/saveOrUpdateAttributeValue", method = RequestMethod.POST)
    public Wrapper<String> saveOrUpdateAttributeValue(@RequestBody AttributeValueRequest attributeValueRequest){
        return Wrapper.success(attributeValueService.saveOrUpdateAttributeValue(this.modelMapper.map(attributeValueRequest, AttributeValue.class)));
    }

    /**
     * 删除属性值
     * @param simpleIdRequest 属性值id
     * @return
     */
    @RequestMapping(value = "deleteAttributeValue", method = RequestMethod.POST)
    public Wrapper<String> deleteAttributeValue(@RequestBody SimpleIdRequest simpleIdRequest){
        return Wrapper.success(attributeValueService.deleteAttributeValue(simpleIdRequest.getId()));
    }

    /**
     * 启用/停用属性值
     * @param simpleStatusRequest
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateAttributeValueStatus", method = RequestMethod.POST)
    public Wrapper<String> updateAttributeValueStatus(@RequestBody SimpleStatusRequest simpleStatusRequest) {
        return Wrapper.success(attributeValueService.updateAttributeValueStatus(simpleStatusRequest.getId(), simpleStatusRequest.getIsEnable()));
    }
}
